<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<!--- configurator.cfc
	CFC loads and manages the application configuration
	created 23 July 07 EJT
	
	NOTES:
	Init with an argument will auto-configure
	Must send full path to the XML file that configs the app
	Expects XMLRoot.XMLChildren[1]...[n] each with XMLAttributes.name/value
	Returns a struct with the name/value pairs as key/values
	 --->


<cfcomponent displayname="Configurator" hint="Loads the application configuration file from XML">
	<cfproperty name="config" type="struct" hint="Struct of name value pairs for application level configuration" />
	<cfset variables.config = structNew() />

	<cffunction name="init" access="public" returntype="Configurator">
		<cfargument name="pathToConfig" type="string" required="false" default="" hint="Path to config XML file">
		<cfif len(arguments.pathToConfig)>
			<cfset this.configure(arguments.pathToConfig) />
		</cfif>
		<cfreturn this>
	</cffunction>
	
	<cffunction name="configure" access="public" returntype="void">
		<cfargument name="pathToConfig" type="string" required="true" hint="Path to config XML file">
		
		<cfset var xmlStr = "" />
		
		<cftry>
			<cfif NOT fileExists(arguments.pathToConfig)>
				<cfthrow type="configurator.config.fileNotFound" message="Configurator CFC: Unable to find path to Config File #arguments.pathToConfig#" />
			</cfif>
			<cfset xmlStr = xmlParse(arguments.pathToConfig, false) />
			
			<cfloop from="1" to="#arrayLen(xmlStr.XmlRoot.XmlChildren)#" index="i">
				<cfset this.config["#xmlStr.XmlRoot.XmlChildren[i].XmlAttributes.name#"] = xmlStr.XmlRoot.XmlChildren[i].XmlAttributes.value />
			</cfloop>
			
			<cfcatch type="any">
				<cfthrow type="configurator.config.xmlParseFailure" message="#cfcatch.Message#" detail="#cfcatch.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfreturn />
	</cffunction>
</cfcomponent>

